﻿using System.Windows;
using System.Windows.Interactivity;

namespace WirelessAccessPoint.Interactivity
{
    /// <summary>
    /// スタイルでビヘイビアとトリガを設定可能とします。
    /// 
    /// 【使用方法】
    /// <Style>
    ///     <Setter Property="interactivity:InteractionForStyle.Behaviors">
    ///         <Setter.Value>
    ///             <interactivity:BehaviorForStyleCollection>
    ///                 ビヘイビア
    ///             </interactivity:BehaviorForStyleCollection>
    ///         </Setter.Value>
    ///     </Setter>
    ///     <Setter Property="interactivity:InteractionForStyle.Triggers">
    ///         <Setter.Value>
    ///             <interactivity:BehaviorForTriggerCollection>
    ///                 トリガ
    ///             </interactivity:BehaviorForTriggerCollection>
    ///         </Setter.Value>
    ///     </Setter>
    /// </Style>
    /// </summary>
    public static class InteractionForStyle
    {
        public static readonly DependencyProperty BehaviorsProperty = DependencyProperty.RegisterAttached(
            "Behaviors",
            typeof(BehaviorForStyleCollection),
            typeof(InteractionForStyle),
            new FrameworkPropertyMetadata(BehaviorsPropertyChanged));

        private static void BehaviorsPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != e.NewValue)
            {
                var behaviorsFromProperty = e.NewValue as BehaviorForStyleCollection;
                var behaviorsFromInteraction = Interaction.GetBehaviors(sender);

                foreach (var behaviorFromProperty in behaviorsFromProperty)
                {
                    behaviorsFromInteraction.Add(behaviorFromProperty.Clone() as System.Windows.Interactivity.Behavior);
                }
            }
        }

        public static BehaviorForStyleCollection GetBehaviors(DependencyObject obj)
        {
            return obj.GetValue(BehaviorsProperty) as BehaviorForStyleCollection;
        }

        public static void SetBehaviors(DependencyObject obj, BehaviorForStyleCollection value)
        {
            obj.SetValue(BehaviorsProperty, value);
        }

        public static readonly DependencyProperty TriggersProperty = DependencyProperty.RegisterAttached(
            "Triggers",
            typeof(TriggerForStyleCollection),
            typeof(InteractionForStyle),
            new FrameworkPropertyMetadata(TriggersPropertyChanged));

        private static void TriggersPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != e.NewValue)
            {
                var triggersFromProperty = e.NewValue as TriggerForStyleCollection;
                var triggersFromInteraction = Interaction.GetTriggers(sender);

                foreach (var triggerFromProperty in triggersFromProperty)
                {
                    var clone = triggerFromProperty.Clone() as System.Windows.Interactivity.TriggerBase;

                    foreach (var actionFromProperty in triggerFromProperty.Actions)
                    {
                        clone.Actions.Add(actionFromProperty.Clone() as System.Windows.Interactivity.TriggerAction);
                    }

                    triggersFromInteraction.Add(clone);
                }
            }
        }

        public static TriggerForStyleCollection GetTriggers(DependencyObject obj)
        {
            return obj.GetValue(BehaviorsProperty) as TriggerForStyleCollection;
        }

        public static void SetTriggers(DependencyObject obj, TriggerForStyleCollection value)
        {
            obj.SetValue(BehaviorsProperty, value);
        }
    }
}
